﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IcbcOpenDemo;
using System.Security.Cryptography;

namespace IcbcOpenDemo.Models
{
    public static class VerifySign
    {
        // 红会
        //public static string publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm00IDv5YQyZWuUqJ/3E0DNHSiM0sQpaB2cj50MnjO89OEQ/E64ovPxo8diLY5PJAbc4LZZF+GHJk2IvWIpUtsDNcKEHiuLK5RalIgGUK7G+fi3yl4QQRjgOPZq09mSZrhu8jsS6kgqfvOnbTy7RaIvfY7Whb8e1LVZu/EhFIyKbUp7GXFDKM6MaxK6+rY72hNPi894m0f2stsSYlg2/FjMTgSWeFuxraGha9MOtb0RLh3DdQBnBZEd0yOk36UtxxzCGEiA0M0SUrwJ9vH4NeZGRw2WJ9vXi2ww8SGqfybTv0llsO/Ql2XewNxtLyNO5NPyRIc9ITYwjXSVtP7pcI8wIDAQAB";
        
        // 测试网关公钥
        public static string publicKey = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwFgHD4kzEVPdOj03ctKM7KV+16bWZ5BMNgvEeuEQwfQYkRVwI9HFOGkwNTMn5hiJXHnlXYCX+zp5r6R52MY0O7BsTCLT7aHaxsANsvI9ABGx3OaTVlPB59M6GPbJh0uXvio0m1r/lTW3Z60RU6Q3oid/rNhP3CiNgg0W6O3AGqwIDAQAB";
        
        //public static string signedString = "IdnKfP5ARUC87eN/eka4VbDFixJqWTxeGvpnpUfqXfojzVTjwpg8l70P2FtayZ6YuGjJpRIF3F3hvF508lFTl6PbA6u62N1HCZ3XFroBjkNajRbYmvR0RDfr0vtOyPOkgOasS+6D7XYutfvPHlsPFpPcSoH+9ZJWyPnKUHkD9abcU4k4sC/83aKk+RcFRg4TFxZGhqKdaD13Bgc16zncm/ZE+2tif5Xk5vOD3MZjqq6yz4S7wv0xFQzE6NmJ4y8H3CagrI1RFGqzGvEwVKzIw+0pYCPA0BrrJFt+/VekatCm7Ssy223fYvPP1ptW7Hex4B7mcTXouUGnfdny64djVg==";
        public static string signedString = @"REVOzQ1+1FHXnMcEspfMP9tRKm4kRFp0r08sxCGC3qzTKVtCYexVLWg+eJkpji4aL4M1yC0pLXBReYMKaM4Qf+bhkDdcTVOqY8zWIMh9dwsTk/SNNg5LsbFaSWnXG214VqStKMknBYGHZPrECZLzKgNUEPvnAYkxDd4IbhhHuVU=";

        //public static string nofyData="eyJ0eENvZGUiOiIwMiIsInJlc3BDb2RlIjoiMDAwMDAiLCJyZXNwTXNnIjoic3VjY2VzcyIsInRyeEFtb3VudCI6MC4wMSwicGF5TWV0aG9kIjoiMDYiLCJvcmRlclN0YXR1cyI6IjAyIiwicmVmdW5kU3RhdHVzIjpudWxsLCJvcmRlcklkIjoiMjAyMTA1MTcxNTU0NDUwMjYiLCJyZWZ1bmRJZCI6bnVsbCwiY29tcGxldGVEYXRlIjoiMjAyMTA1MTciLCJjb21wbGV0ZVRpbWUiOiIxNTo1NToyMCIsImFwcElkIjoiMTAwMDAwMDAwMDAwMDMxNzg2MjMiLCJ2ZW5kb3JJZCI6InpzaHNoMDAwMCIsInVzZXJJZCI6IjEwMDAwMDAwMDAwMDAwMDAzIiwibWVyTmFtZSI6IuS4reWxseW4gue6ouWNgeWtl+S8miIsInN1Yk1lck5hbWUiOiLkuK3lsbHluILnuqLljYHlrZfkvJoiLCJvcmRlck5vIjpudWxsLCJyZWFsVW5mcmVlemVBbXQiOm51bGwsInZlbmRvckRpc2NvdW50IjpudWxsLCJiYW5rRGlzY291bnQiOm51bGwsImlzSmZ0RGlzY291bnQiOiIwIiwiamZ0RGlzY291bnRBbXQiOjAuMDAsInNob3BDb2RlIjpudWxsLCJpY2JjT3JkZXJJZCI6IjIwMTExNDAyMDA1ODAwMDA0MjEwNTE3MDAwNjE4MSIsImNvbW1lbnQiOm51bGwsImV4dGVuc2lvbiI6bnVsbCwiZXJyb3JDb2RlIjpudWxsLCJzdWJPcmRlckluZm9MaXN0IjpudWxsLCJzdWJSZWZ1bmRzIjpudWxsfQ==";

        public static string content = "{\"response_biz_content\":{\"bank_code\":\"102\",\"login_id\":\"kzwlapics.y.2011\",\"tran_date\":\"20210518\",\"rd\":[{\"frozen_amt\":0,\"account_property\":\"2\",\"bank_type\":\"102\",\"account_no\":\" 2011028019200097202\",\"hold_amt\":0,\"last_tran_date\":\"20210531\",\"yesterday_balance\":49358724,\"cash_exf\":\"0\",\"balance\":49358717,\"account_name\":\"颗馈裂蜒裁题焙人化僵侦柱\",\"currency\":\"CNY\",\"i_ret_code\":0,\"i_seq_no\":\"1\",\"last_intr_date\":\"20210321\",\"sub_no\":\"\",\"i_ret_msg\":\"\",\"usable_balance\":49358717,\"query_time\":\"20210531152803303\"}],\"trans_code\":\"QACCBAL\",\"f_seq_no\":\"ZSICBC202105181528031411\",\"group_id\":\"201190001288646\",\"return_msg\":\"\",\"tran_time\":\"152803141\",\"msg_id\":\"ZSICBC202105181528031422\",\"return_code\":0}";

        /// <summary>
        /// 验签
        /// </summary>
        /// <param name="content">待验签字符串</param>
        /// <param name="signedString">签名</param>
        /// <param name="publicKey">公钥</param>
        /// <param name="input_charset">编码格式</param>
        /// <returns>true(通过)，false(不通过)</returns>
        /// public static bool verifySHA256(string content, string signedString, string publicKey, string input_charset)
        public static void VerifySignString()
        {
            bool result = false;
            //byte[] Data = Encoding.GetEncoding("UTF-8").GetBytes(content);
            byte[] Data = Encoding.GetEncoding("UTF-8").GetBytes(content);
            byte[] data = Convert.FromBase64String(signedString);

            RSAParameters paraPub = ConvertFromPublicKey(publicKey);
            RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
            
            rsaPub.ImportParameters(paraPub);            
            SHA256CryptoServiceProvider sh = new SHA256CryptoServiceProvider();            
            
            result = rsaPub.VerifyData(Data, sh, data);

            Console.WriteLine(result);
        }

        public static void Decrypt()
        {   
            byte[] data = Convert.FromBase64String(signedString);
            RSAParameters paraPub = ConvertFromPublicKey(publicKey);
            RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();
            
            rsaPub.ImportParameters(paraPub);
            SHA256CryptoServiceProvider sh = new SHA256CryptoServiceProvider();

            byte[] decryptByte = rsaPub.Decrypt(data, true);

            string stmp = Encoding.UTF8.GetString(decryptByte);
            Console.WriteLine(stmp);
            //rsaPub.ImportParameters(paraPub);
            //SHA256CryptoServiceProvider sh = new SHA256CryptoServiceProvider();

            
        }

        private static RSAParameters ConvertFromPublicKey(string pemFileConent)
            {
                byte[] keyData = Convert.FromBase64String(pemFileConent);
                if (keyData.Length < 162)
                {
                    throw new ArgumentException("pem file content is incorrect.");
                }
                byte[] pemModulus = new byte[128];
                byte[] pemPublicExponent = new byte[3];
                Array.Copy(keyData, 29, pemModulus, 0, 128);
                Array.Copy(keyData, 159, pemPublicExponent, 0, 3);
                RSAParameters para = new RSAParameters();
                para.Modulus = pemModulus;
                para.Exponent = pemPublicExponent;
                return para;
            }
    }
}
